import type { LoginRequestData } from 'src/types/login'

import { MockMethod, Recordable } from 'vite-plugin-mock'
import { accessTokens, vertifyToken } from './utils'

interface Config {
  url: Recordable
  body: Recordable
  query: Recordable
  headers: Recordable
}

export default [
  {
    url: '/api/v1/login',
    method: 'post',
    response: (config: Config) => {
      const body = config.body
      const { username, code } = body as LoginRequestData
      const accessToken = accessTokens[username]
      if (!accessToken) {
        return {
          code: 500,
          message: '帐户或密码不正确。'
        }
      }
      const reg = /^v3admin$/i
      if (!reg.test(code))
        return {
          code: 500,
          message: '验证码不正确'
        }
      return {
        code: 0,
        message: '登录成功',
        data: {
          id: 1,
          name: username,
          token: accessToken
        }
      }
    }
  },
  {
    url: '/api/v1/captcha',
    method: 'get',
    response: () => {
      return {
        code: 0,
        message: '获取验证码成功',
        data: {
          captcha: 'http://dummyimage.com/100x40/dcdfe6/000000.png&text=V3Admin'
        }
      }
    }
  },
  {
    url: '/api/v1/userInfo',
    method: 'get',
    response: ({ headers }: Config) => {
      const obj = vertifyToken(headers)
      if (obj.code) return obj
      return {
        code: 0,
        message: '获取用户详情成功',
        data: {
          roles: obj.roles,
          username: obj.username,
          'avatar|1': [
            'https://i.gtimg.cn/club/item/face/img/2/15922_100.gif',
            'https://i.gtimg.cn/club/item/face/img/8/15918_100.gif'
          ]
        }
      }
    }
  }
] as MockMethod[]
